package com.test.reggieadmin.model.login;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.test.reggieadmin.R;
import com.test.reggieadmin.base.BaseActivity;
import com.test.reggieadmin.bean.EmployeeBean;
import com.test.reggieadmin.bean.State;
import com.test.reggieadmin.databinding.ActivityLoginBinding;
import com.test.reggieadmin.http.API;
import com.test.reggieadmin.model.home.MainActivity;
import com.test.reggieadmin.model.ip.IPSettingActivity;
import com.yechaoa.yutils.GsonUtil;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginPrensenter> implements
        LoginView, View.OnClickListener {

    private int SET = 201;

    @Override
    protected LoginPrensenter createPresenter() {
        return new LoginPrensenter(this);
    }

    @Override
    protected void initView() {
        viewBinding.btnLogin.setOnClickListener(this::onClick);
        viewBinding.llSettingIp.setOnClickListener(this::onClick);

        if (API.kv.decodeString(State.ip) == null || TextUtils.isEmpty(API.kv.decodeString(State.ip))) {
            Intent intent = new Intent(this, IPSettingActivity.class);
            startActivityForResult(intent, SET);
        } else {
            if (API.kv.decodeBool(State.isLogin)) {//已登录
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void loginData(EmployeeBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            startActivity(new Intent(this, MainActivity.class));
            //保存用户信息
            API.kv.encode(State.employee, GsonUtil.GsonString(bean));
            //保存登录状态
            API.kv.encode(State.isLogin, true);
            finish();
        }
    }

    @Override
    public void onError(String msg) {
        YUtils.hideLoading();
        ToastUtil.showCenter(msg);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SET && resultCode == Activity.RESULT_OK) {
            //退出程序并杀死进程
            ActivityManager manager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            manager.restartPackage("com.test.reggieadmin");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_setting_ip://设置IP
                Intent intent = new Intent(this, IPSettingActivity.class);
                startActivityForResult(intent, SET);
                break;
            case R.id.btn_login://登录
                if (TextUtils.isEmpty(viewBinding.etUsername.getText().toString().trim())) {
                    ToastUtil.showCenter("用户名不能为空");
                } else if (TextUtils.isEmpty(viewBinding.etPassword.getText().toString().trim())) {
                    ToastUtil.showCenter("密码不能为空");
                } else {
                    YUtils.showLoading("提交中...");
                    Map<String, Object> mapLogin = new HashMap<>();
                    mapLogin.put("username", viewBinding.etUsername.getText().toString().trim());//用户名
                    mapLogin.put("password", viewBinding.etPassword.getText().toString().trim());//密码
                    presenter.login(mapLogin);//登录
                }
                break;
            default:
                break;
        }
    }
}